package cn.rmc.hrm.service.impl;

import cn.rmc.hrm.To.TenantTo;
import cn.rmc.hrm.constants.BaseConstant;
import cn.rmc.hrm.domain.Employee;
import cn.rmc.hrm.domain.Login;
import cn.rmc.hrm.domain.Tenant;
import cn.rmc.hrm.exception.errorcode.ErrorCode;
import cn.rmc.hrm.feign.LoginFeign;
import cn.rmc.hrm.mapper.EmployeeMapper;
import cn.rmc.hrm.mapper.TenantMapper;
import cn.rmc.hrm.service.ITenantService;
import cn.rmc.hrm.to.LoginMealTo;
import cn.rmc.hrm.util.AjaxResult;
import cn.rmc.hrm.util.AssertUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rmc
 * @since 2021-08-15
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private LoginFeign loginFeign;
    @Autowired
    private  EmployeeMapper employeeMapper;
    @Value("${expire.days}")
    private int days;

    @Override
    public void entering(TenantTo tenantTo) {
//        获取店铺
        Tenant tenant = tenantTo.getTenant();
//        获取用户
        Employee employee = tenantTo.getEmployee();
//        1.判断参数
        checkParam(tenant,employee);
//        3.保存登录信息
        Login login = new Login();
        login.setUsername(tenantTo.getUsername());
        login.setPassword(tenantTo.getPassword());
        login.setType(BaseConstant.LoginConstant.TYPE_TENANT);
        AjaxResult ajaxResult = loginFeign.save(login);
//        判读结果是否为空
        AssertUtils.isNull(ajaxResult.getResultObj(),ErrorCode.OPER_FAIL.getMessage());
//        通过前台对象获取id
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
//        5.保存员工信息
        employee.setRealName(tenantTo.getUsername());
        Date now = new Date();
        employee.setInputTime(now);
        employee.setState(BaseConstant.EmployeeConstant.STATE_NORMAL);
        employee.setLoginId(loginId);
        employee.setType(BaseConstant.EmployeeConstant.TYPE_TENANT_ADMIN);
        employeeMapper.insert(employee);
//       6.保存机构
        tenant.setAdminId(employee.getId());
        tenant.setRegisterTime(now);
        tenant.setState(BaseConstant.TenantConstant.STATE_TO_AUDIT);
        tenantMapper.insert(tenant);
//        将店铺id更新到用户信息
        employee.setTenantId(tenant.getId());
        employeeMapper.updateById(employee);
//        7.保存购买套餐信息
        LoginMealTo loginMealTo = new LoginMealTo();
        loginMealTo.setLoginId(loginId);
        loginMealTo.setMealId(tenantTo.getMealId());
        loginMealTo.setExpireDate(DateUtils.addDays(now,days));// 设置默认7天
        loginMealTo.setState(BaseConstant.MealConstant.STATE_TO_PAY);
        loginFeign.saveLoginMeal(loginMealTo);
    }

    private void checkParam(Tenant tenant,Employee employee) {
//        判断商户名字是否为空
        AssertUtils.isNull(tenant.getCompanyName(), ErrorCode.TENANT_NAME_NOT_NULL.getMessage());
//        判断电话号码，企业编码是否合法
        AssertUtils.isNotLicense(tenant.getCompanyNum(),ErrorCode.CREDIT_CODE_ILLEGAL.getMessage());
        AssertUtils.isNotMobile(employee.getTel(),ErrorCode.PHONE_ILLEGAL.getMessage());
//        判断机构是否入驻
       AssertUtils.isTrue( tenantMapper.loadByTenantNum(tenant.getCompanyNum()),ErrorCode.TENANT_IS_EXIST.getMessage());
    }
}
